//
//  XinLiCePingTestListVC.m
//  students
//
//  Created by 任辰东 on 2024/3/4.
//

#import "XinLiCePingTestListVC.h"
#import "XinLiCePingTestTCell.h"
#import "XinLiCePingApis.h"
#import "RCDCornerBtn.h"

@interface XinLiCePingTestListVC ()

@property (nonatomic, strong) XinLiCePingListModel *model;

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) RCDCornerBtn *submitBtn;

@property (nonatomic, strong) XinLiCePingDetailModel *detailModel;

@end

@implementation XinLiCePingTestListVC

-(instancetype)initWithModel:(XinLiCePingListModel *)model
{
    self = [super init];
    if (self) {
        _model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.model.name;
    
    [self setupEstimatedRowHeight:200 cellClasses:@[[XinLiCePingTestTCell class]]];
    [self beginRefresh];
    
    self.myTableView.mj_footer = nil;
    
    if (!self.model.is_answer.boolValue) {
        
        [self.view addSubview:self.bottomView];
        [self.bottomView addSubview:self.submitBtn];
        
        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.bottom.right.equalTo(self.view);
        }];
        
        [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.bottomView).offset(10);
            make.left.equalTo(self.bottomView).offset(15);
            make.right.equalTo(self.bottomView).offset(-15);
            make.bottom.equalTo(self.bottomView.mas_bottom).offset(kBottomOffset);
            make.height.mas_equalTo(40);
        }];
        
        [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.left.right.equalTo(self.view);
            make.bottom.equalTo(self.bottomView.mas_top);
        }];
        
        WeakSelf
        [self.submitBtn setClickBlock:^(UIButton * _Nonnull btn) {
             StrongSelf
            DLog(@"提交");
            
            for (NSInteger i = 0; i < self.dataSourceArr.count; i ++) {
                
                XinLiCePingModel *model = self.dataSourceArr[i];
                if (IsEmpty(model.daan)) {
                    [self showTextHUDWithMessage:@"还有未完成题目,暂无法提交"];
                    return;
                }
            }
            
            [self submit];
            DLog(@"提交");
        }];
    }else{
        [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.left.right.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(kBottomOffset);
        }];
    }
}

-(void)submit
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"liangbiao_id"] = self.model.liangbiao_id;
    params[@"student_id"] = [GVUserDefaults standardUserDefaults].student_id;
    params[@"student_name"] = [GVUserDefaults standardUserDefaults].student_name;
    params[@"organ_id"] = [GVUserDefaults standardUserDefaults].organ_id;
    params[@"organ_name"] = [GVUserDefaults standardUserDefaults].organ_name;
    params[@"class_id"] = [GVUserDefaults standardUserDefaults].class_id;
    params[@"class_name"] = [GVUserDefaults standardUserDefaults].class_name;
    params[@"grade"] = @"0";
    params[@"grade_name"] = @"0";
    params[@"main_id"] = self.model.main_id;
    
    NSMutableArray *fenxi_set = [NSMutableArray array];
    
    for (NSInteger i = 0; i < self.detailModel.fenxi_set.count; i ++) {
        
        XinLiCePingFenxiModel *fenxiModel = self.detailModel.fenxi_set[i];
        NSMutableDictionary *fenxiParams = [NSMutableDictionary dictionary];
        fenxiParams[@"yinzi_id"] = fenxiModel.yinzi_id;
        fenxiParams[@"yinzi_name"] = fenxiModel.yinzi_name;
        fenxiParams[@"yinzi_type"] = fenxiModel.yinzi_type;
        fenxiParams[@"yinzi_jieguo"] = fenxiModel.yinzi_jieguo;
        fenxiParams[@"yinzi_jiancheng"] = fenxiModel.yinzi_jiancheng;
        
        fenxiParams[@"red_num"] = fenxiModel.red_num;
        fenxiParams[@"cheng_num"] = fenxiModel.cheng_num;
        fenxiParams[@"huang_num"] = fenxiModel.huang_num;
        fenxiParams[@"lan_num"] = fenxiModel.lan_num;
        fenxiParams[@"linjie_num"] = IsEmpty(fenxiModel.linjie_num)?@"":fenxiModel.linjie_num;
        
        NSMutableArray *yinzi_fenxi = [NSMutableArray array];
        for (XinLiCePingYinziFenxiModel *yinxiModel in fenxiModel.yinzi_fenxi) {
                            
            NSMutableDictionary *yinziParams = [NSMutableDictionary dictionary];
            yinziParams[@"shuoming"] = yinxiModel.shuoming;
            yinziParams[@"min"] = yinxiModel.min;
            yinziParams[@"max"] = yinxiModel.max;
            yinziParams[@"jianyi"] = yinxiModel.jianyi;
            yinziParams[@"jieguo"] = yinxiModel.jieguo;
            [yinzi_fenxi addObject:yinziParams];
        }
        fenxiParams[@"yinzi_fenxi"] = yinzi_fenxi;
        [fenxi_set addObject:fenxiParams];
    }
    
    NSMutableArray *ti_answer = [NSMutableArray array];
    
    for (NSInteger i = 0; i < self.detailModel.content.count; i ++) {
        
        XinLiCePingModel *model = self.detailModel.content[i];
        
//        if (model.fenxiModel) {
//            XinLiCePingFenxiModel *fenxiModel = model.fenxiModel;
//            
//            NSMutableDictionary *fenxiParams = [NSMutableDictionary dictionary];
//            fenxiParams[@"yinzi_id"] = fenxiModel.yinzi_id;
//            fenxiParams[@"yinzi_name"] = fenxiModel.yinzi_name;
//            fenxiParams[@"yinzi_type"] = fenxiModel.yinzi_type;
//            fenxiParams[@"yinzi_jieguo"] = fenxiModel.yinzi_jieguo;
//            fenxiParams[@"yinzi_jiancheng"] = fenxiModel.yinzi_jiancheng;
//            
//            fenxiParams[@"red_num"] = fenxiModel.red_num;
//            fenxiParams[@"cheng_num"] = fenxiModel.cheng_num;
//            fenxiParams[@"huang_num"] = fenxiModel.huang_num;
//            fenxiParams[@"lan_num"] = fenxiModel.lan_num;
//            fenxiParams[@"linjie_num"] = IsEmpty(fenxiModel.linjie_num)?@"":fenxiModel.linjie_num;
//            
//            NSMutableArray *yinzi_fenxi = [NSMutableArray array];
//            for (XinLiCePingYinziFenxiModel *yinxiModel in fenxiModel.yinzi_fenxi) {
//                                
//                NSMutableDictionary *yinziParams = [NSMutableDictionary dictionary];
//                yinziParams[@"shuoming"] = yinxiModel.shuoming;
//                yinziParams[@"min"] = yinxiModel.min;
//                yinziParams[@"max"] = yinxiModel.max;
//                yinziParams[@"jianyi"] = yinxiModel.jianyi;
//                yinziParams[@"jieguo"] = yinxiModel.jieguo;
//                [yinzi_fenxi addObject:yinziParams];
//            }
//            fenxiParams[@"yinzi_fenxi"] = yinzi_fenxi;
//            [fenxi_set addObject:fenxiParams];
//        }
        
        NSMutableDictionary *ti_answerParams = [NSMutableDictionary dictionary];
        ti_answerParams[@"ti_id"] = model.ti_id;
        ti_answerParams[@"ti_title"] = model.ti_title;
        ti_answerParams[@"yinzi_id"] = model.yinzi_id;
        ti_answerParams[@"daan"] = model.daan;
        ti_answerParams[@"defen"] = model.defen;
        
        NSMutableArray *options = [NSMutableArray array];
        
        for (XinLiCePingOptionsModel *optionModel in model.options) {
            
            NSMutableDictionary *optionsParams = [NSMutableDictionary dictionary];
            optionsParams[@"options_name"] = optionModel.options_name;
            optionsParams[@"options_score"] = optionModel.options_score;
            optionsParams[@"options_title"] = optionModel.options_title;
            [options addObject:optionsParams];
        }
        ti_answerParams[@"options"] = options;
        [ti_answer addObject:ti_answerParams];
    }
    params[@"fenxi_set"] = fenxi_set;
    params[@"ti_answer"] = ti_answer;
    
    DLog(@"params === %@",params);
    
    WeakSelf
    [XinLiCePingApis xxb_XinliDatiRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        [self showCompletionHUDWithMessage:msg completion:^{
            StrongSelf
            if (self.SubmitSuccessBlock) {
                self.SubmitSuccessBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }];
        
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

-(void)requestDataWithOffset:(NSInteger)offset success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure
{
    NSDictionary *params = @{
        @"is_answer":self.model.is_answer,
        @"student_id":[GVUserDefaults standardUserDefaults].student_id,
        @"main_id":self.model.main_id
//        @"is_answer":@"1",
//        @"main_id":@"54",
//        @"student_id":@"5139"
    };
    
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [XinLiCePingApis getXinliDetailRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        DLog(@"responseData === %@",responseData);
        if (self.model.is_answer.boolValue) {
            NSMutableArray *data = [XinLiCePingModel mj_objectArrayWithKeyValuesArray:responseData];
            [data enumerateObjectsUsingBlock:^(XinLiCePingModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                obj.is_answer = self.model.is_answer;
            }];
            success(data);
        } else {
            
            XinLiCePingDetailModel *model = [XinLiCePingDetailModel mj_objectWithKeyValues:responseData];
            self.detailModel = model;
            success(model.content);
        }

        [self addEmptyView];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
    StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
   
}

- (void)configCellSubViewsCallback:(XinLiCePingTestTCell *)cell indexPath:(NSIndexPath *)indexPath
{
    XinLiCePingModel *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }
    
    WeakSelf
    [cell setSelectedBlock:^(XinLiCePingOptionsModel * _Nonnull smodel) {
        StrongSelf
        model.daan = smodel.options_name;
        model.defen = smodel.options_score;
        for (XinLiCePingFenxiModel *fenxiModel in self.detailModel.fenxi_set) {
            
            if ([fenxiModel.yinzi_id isEqualToString:model.yinzi_id]) {
                
                for (XinLiCePingYinziFenxiModel *yinziModel in fenxiModel.yinzi_fenxi) {
                    
                    if (smodel.options_score.integerValue >= yinziModel.min.integerValue && smodel.options_score.integerValue <= yinziModel.max.integerValue) {
                        
                        fenxiModel.yinzi_jieguo = yinziModel.jieguo;
                        fenxiModel.yinzi_type = self.detailModel.fenxi_type;
                    }
                }
                model.fenxiModel = fenxiModel;
            }
        }
    }];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XinLiCePingModel *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }

}

-(void)addEmptyView
{
    if (IsEmpty(self.dataSourceArr)) {
        [RCDEmptyView addEmptyWithStyle:EmptyViewStyleCommon onView:self.myTableView position:EmptyViewPositionCenter clickCallBack:nil];
    }else{
        [RCDEmptyView removeFromParentView:self.myTableView];
    }
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
        _bottomView.backgroundColor = UIColorFromHex(0xffffff);
    }
    return _bottomView;
}

-(RCDCornerBtn *)submitBtn
{
    if (!_submitBtn) {
        _submitBtn = [RCDCornerBtn new];
        [_submitBtn setTitleString:@"提交"];
        [_submitBtn setTitleFont:16];
        _submitBtn.cornerRadius = 20;
    }
    return _submitBtn;
}


@end
